package cn.z2huo.algorithm.stack.lineEditor;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * 行编辑程序
 * @author bouncyslime
 * @date 2020/10/25
 */
public class LineEditor {
    public static void main(String[] args) {
        new LineEditor().lineEdit("whli##ilr#e(s#*s)\n\toutcha@putchar(*s=#++);");
    }

    public void lineEdit(String string){
        Scanner scanner = new Scanner(string);
        Deque<String> stack = new LinkedList<>();
        StringBuilder sb = new StringBuilder();
        String str = null;
        str = scanner.next();
        System.out.println(str);
        while (!"~".equals(str)){
            while (!"~".equals(str) && !"\n".equals(str)){
                switch (str){
                    case "#":
                        stack.pop();
                        break;
                    case "@":
                        stack.clear();
                        break;
                    default:
                        stack.push(str);
                        break;
                }
                str = scanner.next();
            }
            for (int i=stack.size()-1; i>=0; i--){
                sb.append(stack.pop());
            }
        }

        System.out.println(sb.reverse());
    }
}
